import {_decorator, Component, EventKeyboard, Input, input, KeyCode, tween, Vec3,AudioClip} from 'cc';

const { ccclass, property } = _decorator;
import CryptoES from "crypto-es";
import {GameMgr} from "db://assets/script/GameMgr";
@ccclass('Player')
export class Player extends Component {
    @property jumpHeight: number = 0;
    @property jumpDuration: number = 0;
    @property maxMoveSpeed: number = 0;
    @property accel: number = 0;
    @property(AudioClip)
    public jumpAudioClip: AudioClip = null!;

    private accLeft: boolean;
    private accRight: boolean;
    private xSpeed: number;
    _curPos: Vec3 = new Vec3();
    private _deltaPos: Vec3 = new Vec3(0, 0, 0);

    start() {


        const data = JSON.stringify({
            "webId": 1003,
            "mailTitle": "《山海经的红包雨?!》网页活动奖励（第1次）",
            "groupId": 1
        })

        // const secretKey = "6ceb308e1748cd4f3f5fcb0da801c92c6e3a5f88194da01f5e27eebc80efbce5"
        //
        // const hmac = CryptoES.createHmac('sha256', secretKey).update(body).digest("base64")
        //
        // console.log(hmac)
        // let data = "{\n" +
        //     "  \"webId\": 1003,\n" +
        //     "  \"mailTitle\": \"《山海经的红包雨?!》网页活动奖励（第1次）\",\n" +
        //     "  \"groupId\": 1\n" +
        //     "}";
        const secret = '6ceb308e1748cd4f3f5fcb0da801c92c6e3a5f88194da01f5e27eebc80efbce5';
        const hmac = CryptoES.HmacSHA256(data, secret);
// 使用16进制的方法加密，输出字符串
        let testStr= CryptoES.enc.Base64.stringify(hmac);
        console.log("===>   "+testStr);


        // const nodeTween = this.runJumpAction();
        // tween(this.node).then(nodeTween).start()
        this.runJumpAction2()
        // 加速度方向开关
        this.accLeft = false;
        this.accRight = false;

        // 主角当前水平方向速度
        this.xSpeed = 0;
        input.on(Input.EventType.KEY_DOWN, this.keyDown, this)
        input.on(Input.EventType.KEY_UP, this.keyUp, this)

        let position = this.node.getPosition();
        this.vec.y = position.y;

        console.log(this.jumpAudioClip);
    }

    update(dt: number) {
        // 根据当前加速度方向每帧更新速度
        if (this.accLeft) {
            this.xSpeed -= this.accel * dt;
        } else if (this.accRight) {
            this.xSpeed += this.accel * dt;
        }

        // 限制主角的速度不能超过最大值
        if (Math.abs(this.xSpeed) > this.maxMoveSpeed) {
            // if speed reach limit, use max speed with current direction
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
        }

        // 根据当前速度更新主角的位置
        // this.node.getPosition(this._curPos);
        let dtX = this.xSpeed * dt;
        let pos = this.node.getPosition();
        this.vec.x = dtX + pos.x
        this.node.setPosition(this.vec.x, this.vec.y, this.vec.z);

    }

    protected onDestroy() {
        console.log("onDestroy------------->")
        input.off(Input.EventType.KEY_DOWN, this.keyDown, this);
        input.off(Input.EventType.KEY_UP, this.keyUp, this);
    }

    // 角色坐标 不直接使用tween修改节点坐标的原因是因为tween.by会每帧修改x，防止冲突
    private vec = { x: 0, y: 0, z: 0 }

    runJumpAction2() {
        const that = this;
        const jumpDown = tween().by(this.jumpDuration, { y:  this.jumpHeight, },{easing: 'sineOut' } )//, { easing: "sineOut" });
        var jumpDUp = tween().by(this.jumpDuration, { y: 0 -this.jumpHeight}, {easing: 'sineIn' } )//, { easing: "sineIn" });
        let sequence1 = tween().sequence(jumpDown, jumpDUp).call(()=>{
            GameMgr.inst.playerSound(that.jumpAudioClip);
            console.log(this +"          =================?")
        })//顺序执行);
        tween(this.vec).then(sequence1)
        .repeatForever().start();//无限重复缓动
    }

    private keyDown(event: EventKeyboard) {
        console.log("keydown")
        // set a flag when key pressed
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                this.accLeft = true;
                break;
            case KeyCode.KEY_D:
                this.accRight = true;
                break;
        }
    }

    private keyUp(event: EventKeyboard) {
        console.log("keyup")

        switch (event.keyCode) {
            case KeyCode.KEY_A:
                this.accLeft = false;
                break;
            case KeyCode.KEY_D:
                this.accRight = false;
                break;
        }
    }



}


